import request from '@/router/axios';

export const getDeptList = (current, size, params) => {
    return request({
        url: '/api/Department/List',
        method: 'get',
        params: {
            ...params,
            current,
            size,
        }
    })
}
export const addDept = (data = {}) => request({
    url: '/api/Department/Add',
    method: 'post',
    data: data
});
export const deleteDept = (params = {}) => request({
    url: '/api/Department/Delete',
    method: 'post',
    params: Object.assign(params, {
    }),
});

export const createDeptTree=(deptList=[])=>{

    var m={};
    deptList.forEach(d=>{
        var pId=   d.parentId
        if(!m[pId]){
            m[pId]=[]
        }
        m[pId].push(d)
    });
    var pArr=m["0"]
    var tree=[]
    if(!pArr) return tree;
    pArr.forEach(p=>{
        var dept={
            label:p.name,
            value:p.id,
            id:p.id,
            children:getChildren(p,m)
        }
        tree.push(dept)
    })
    return tree;

}
export const getChildren=(p,m)=>{
    var id=p.id
    var childs=m[id];
    if(!childs) return [];
    var children=[]
    childs.forEach(c=>{
        var d={
            label:c.name,
            value:c.id,
            id:c.id,
            children:getChildren(c,m)
        }
        children.push(d)

    })

    return children;
}
export const createCascadDepValue=(csv,deptList)=>{
    var m={};
    deptList.forEach(p=>{
        m[p.id]=p;
    })
    var result=[]
    if(csv){
        var deptArr=csv.split(",");//["id1","id2"]
        deptArr.map(s=>{
            var arr=[];
            createParentArr(m,s,arr);
            result.push(arr);
        })
    }
    return result;

}
export const createParentArr=(m,str,arr)=>{
    var d=m[str];
    if(!d) {
        return arr;
    }
    arr.unshift(d.id);
    var pId=d.parentId;
    if(pId=="0"){
        return arr;
    }
    createParentArr(m,pId,arr);
}